﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MovieRental_Presentation.Helpers;
using MovieRental.EnhancedEntities;
using System.Collections.ObjectModel;
using MovieRental.Entities;

namespace MovieRental_Presentation.DataViews
{
    public class DashboardDataView : BaseDataView
    {
        private ObservableCollection<LoanCart> _carts = new ObservableCollection<LoanCart>();
        private ObservableCollection<Edition> _onSaleEditions = new ObservableCollection<Edition>();
        private DateTime _selectedDate;
        private string _searchString;

        public DashboardDataView()
        {
            ComponentManager.Instance.TemporaryDiscountComponent.SourceUpdated += MakeEventHandlerForProperty("OnSaleEditions");
            ComponentManager.Instance.LoanCartComponent.SourceUpdated += MakeEventHandlerForProperty("Income");
            ComponentManager.Instance.LoanCartComponent.SourceUpdated += MakeEventHandlerForProperty("LoansCount");
            ComponentManager.Instance.LoanCartComponent.SourceUpdated += MakeEventHandlerForProperty("Carts");
        }

        public DateTime SelectedDate
        {
            set 
            {
                SetProperty<DateTime>(ref _selectedDate, value, "SelectedDate");
                OnPropertyChanged("Carts");
            }
            get { return _selectedDate; }
        }

        public string SearchString
        {
            set
            {
                SetProperty<string>(ref _searchString, value, "SearchString");
                OnPropertyChanged("Carts");
            }
            get
            {
                return _searchString;
            }
        }

        public ObservableCollection<LoanCart> Carts
        {
            get
            {
                var query = ComponentManager.Instance.LoanCartComponent.GetAllNotReturnedForDay(SelectedDate, SearchString).Select(x => new LoanCart(x));
                UpdateObservableCollection<LoanCart>(_carts, query);
                return _carts;
            }
        }

        public ObservableCollection<Edition> OnSaleEditions
        {
            get
            {
                var query = ComponentManager.Instance.EditionComponent.GetTopDiscountEditions(10).Select(x => new Edition(x));
                UpdateObservableCollection<Edition>(_onSaleEditions, query);
                return _onSaleEditions;
            }
        }

        public double Income
        {
            get { return ComponentManager.Instance.LoanCartComponent.GetIncome(DateTime.Today); }
        }

        public int LoansCount
        {
            get { return ComponentManager.Instance.LoanCartComponent.GetLoanCount(DateTime.Today); }
        }

        
    }
}
